package com.jackie.easyble.ui.dialogs

import android.content.Context
import android.os.Bundle
import android.os.ParcelUuid
import android.view.View
import android.widget.AdapterView
import com.jackie.easyble.R
import com.jackie.easyble.databinding.DialogAddAdvertiseDataBinding
import com.jackie.easyble.databinding.DialogAddAdvertiseUuidBinding
import com.sscl.baselibrary.dialog.BaseDataBindingDialog
import com.sscl.baselibrary.textwatcher.HexTextAutoAddEmptyCharInputWatcher
import com.sscl.baselibrary.utils.ToastUtil
import com.sscl.baselibrary.utils.fromHexStringWithSpaceToByteArray
import com.sscl.easyble.advertise.AdvertiseUuid
import com.sscl.easyble.extensions.toBluetoothGattFullUUID
import java.util.UUID

/**
 * 选择广播数据类型的对话框
 */
class AddAdvertiseUuidDialog(
    context: Context,
    private val confirmButtonClick: (advertiseUuid: AdvertiseUuid) -> Unit
) :
    BaseDataBindingDialog<DialogAddAdvertiseUuidBinding>(context) {

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     *
     * 属性声明
     *
     * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    /* * * * * * * * * * * * * * * * * * * 可空属性 * * * * * * * * * * * * * * * * * * */

    private var currentTextWatcher: HexTextAutoAddEmptyCharInputWatcher? = null

    /* * * * * * * * * * * * * * * * * * * 常量属性 * * * * * * * * * * * * * * * * * * */

    /**
     * 下拉列表选中变化回调
     */
    private val onItemSelectedListener = object : AdapterView.OnItemSelectedListener {

        override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
            when (position) {
                0 -> {
                    binding.uuidEt.removeTextChangedListener(currentTextWatcher)
                    currentTextWatcher = HexTextAutoAddEmptyCharInputWatcher(binding.uuidEt, 2)
                    binding.uuidEt.addTextChangedListener(currentTextWatcher)
                }

                1 -> {

                    binding.uuidEt.removeTextChangedListener(currentTextWatcher)
                    currentTextWatcher = HexTextAutoAddEmptyCharInputWatcher(binding.uuidEt, 4)
                    binding.uuidEt.addTextChangedListener(currentTextWatcher)
                }

                2 -> {
                    binding.uuidEt.removeTextChangedListener(currentTextWatcher)
                    currentTextWatcher = HexTextAutoAddEmptyCharInputWatcher(binding.uuidEt, 16)
                    binding.uuidEt.addTextChangedListener(currentTextWatcher)
                }
            }
        }

        override fun onNothingSelected(parent: AdapterView<*>?) {

        }

    }

    /**
     * 点击事件的监听
     */
    private val onClickListener = View.OnClickListener {
        when (it.id) {
            binding.cancelBtn.id -> {
                dismiss()
            }

            binding.confirmBtn.id -> {
                val uuidString = binding.uuidEt.text.toString().trim().replace(" ", "")
                if (uuidString.isEmpty()) {
                    ToastUtil.toastShort(context, R.string.uuid_empty)
                    return@OnClickListener
                }
                val uuidDataString = binding.uuidDadaEt.text.toString().trim()
                if (uuidDataString.isEmpty()) {
                    ToastUtil.toastShort(context, R.string.uuid_data_empty)
                    return@OnClickListener
                }
                val position = binding.advertiseUuidTypeSpinner.selectedItemPosition
                val uuid = when (position) {
                    0 -> {
                        try {
                            uuidString.toBluetoothGattFullUUID()
                        } catch (e: Exception) {
                            ToastUtil.toastShort(context, e.message ?: "")
                            return@OnClickListener
                        }
                    }

                    1 -> {
                        try {
                            uuidString.toBluetoothGattFullUUID()
                        } catch (e: Exception) {
                            ToastUtil.toastShort(context, e.message ?: "")
                            return@OnClickListener
                        }
                    }

                    else -> {
                        UUID.fromString(uuidString)
                    }
                }
                val uuidData =
                    uuidDataString.fromHexStringWithSpaceToByteArray()
                confirmButtonClick(AdvertiseUuid(ParcelUuid(uuid), uuidData))
                dismiss()
            }
        }
    }

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    *
    * 抽象方法
    *
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

    override fun getLayout(): Int {
        return R.layout.dialog_add_advertise_uuid
    }

    override fun setBinding() {

    }

    override fun doBeforeInitOthers() {

    }

    override fun initViewData(savedInstanceState: Bundle?) {
        currentTextWatcher = HexTextAutoAddEmptyCharInputWatcher(binding.uuidEt, 2)
        binding.uuidEt.addTextChangedListener(currentTextWatcher)
        binding.uuidDadaEt.addTextChangedListener(
            HexTextAutoAddEmptyCharInputWatcher(
                binding.uuidDadaEt,
                29
            )
        )
    }

    override fun initOtherData() {

    }

    override fun initEvents() {
        binding.cancelBtn.setOnClickListener(onClickListener)
        binding.confirmBtn.setOnClickListener(onClickListener)
        binding.advertiseUuidTypeSpinner.onItemSelectedListener = onItemSelectedListener
    }

    override fun doAfterAll() {

    }
}